package com.yongcheon.sms_manager.data.phone_spsm;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.CursorAdapter;

import com.yongcheon.sms_manager.vo.PhoneVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-24.
 */
public abstract class AbstractPhoneData {

    public abstract CursorAdapter getCurosrAdapter(Context context, SQLiteDatabase database);
    public abstract void insert(PhoneVO vo, Context context, SQLiteDatabase database);
    public abstract void delete(long id, Context context, SQLiteDatabase database);
    public abstract void deleteByPhoneNum(String num, Context context, SQLiteDatabase database);
    public abstract ArrayList<PhoneVO> select(String poneNum, Context context, SQLiteDatabase database);

    public ArrayList<PhoneVO> getPhoneListByNum(Cursor cursor){
        ArrayList<PhoneVO> array = new ArrayList<PhoneVO>();
        if(cursor != null) {
            while (cursor.moveToNext()) {
                array.add(createPhoneVOFromCursor(cursor));
            }
            cursor.close();
        }
        return array;
    }

    public PhoneVO createPhoneVOFromCursor(Cursor cursor){
        PhoneVO vo = new PhoneVO();
        setPhoneVO(vo, "number",  cursor);
        setPhoneVO(vo, "voicemail_uri",  cursor);
        setPhoneVO(vo, "features",  cursor);
        setPhoneVO(vo, "new",  cursor);
        setPhoneVO(vo, "formatted_number",  cursor);
        setPhoneVO(vo, "numbertype",  cursor);
        setPhoneVO(vo, "date",  cursor);
        setPhoneVO(vo, "duration",  cursor);
        setPhoneVO(vo, "data_usage",  cursor);
        setPhoneVO(vo, "presentation",  cursor);
        setPhoneVO(vo, "numberlabel",  cursor);
        setPhoneVO(vo, "geocoded_location",  cursor);
        setPhoneVO(vo, "lookup_uri",  cursor);
        setPhoneVO(vo, "countryiso",  cursor);
        setPhoneVO(vo, "transcription",  cursor);
        setPhoneVO(vo, "subscription_component_name",  cursor);
        setPhoneVO(vo, "name",  cursor);
        setPhoneVO(vo, "matched_number",  cursor);
        setPhoneVO(vo, "subscription_id",  cursor);
        setPhoneVO(vo, "photo_id",  cursor);
        setPhoneVO(vo, "normalized_number",  cursor);
        setPhoneVO(vo, "_id",  cursor);
        setPhoneVO(vo, "type",  cursor);
        setPhoneVO(vo, "is_read",  cursor);
        setPhoneVO(vo, "spam_desc",  cursor);
        setPhoneVO(vo, "spam_category",  cursor);
        return vo;
    }

    public void setPhoneVO(PhoneVO vo, String column, Cursor cursor){
        int colIdx = cursor.getColumnIndex(column);
        if(colIdx != -1) {
            if(column.equals("number")) {
                vo.setNumber(cursor.getString(colIdx));
            }else if(column.equals("voicemail_uri")) {
                vo.setVoicemail_uri(cursor.getString(colIdx));
            }else if(column.equals("features")) {
                vo.setFeatures(cursor.getInt(colIdx));
            }else if(column.equals("new")) {
                vo.set_new(cursor.getInt(colIdx));
            }else if(column.equals("formatted_number")) {
                vo.setDate(cursor.getLong(colIdx));
            }else if(column.equals("numbertype")) {
                vo.setFormatted_number(cursor.getString(colIdx));
            }else if(column.equals("date")) {
                vo.setDate(cursor.getLong(colIdx));
            }else if(column.equals("duration")) {
                vo.setDuration(cursor.getLong(colIdx));
            }else if(column.equals("data_usage")) {
                vo.setType(cursor.getInt(colIdx));
            }else if(column.equals("presentation")) {
                vo.setPresentation(cursor.getInt(colIdx));
            }else if(column.equals("numberlabel")) {
                vo.setNumberlabel(cursor.getString(colIdx));
            }else if(column.equals("geocoded_location")) {
                vo.setGeocoded_location(cursor.getString(colIdx));
            }else if(column.equals("lookup_uri")) {
                vo.setLookup_uri(cursor.getString(colIdx));
            }else if(column.equals("countryiso")) {
                vo.setCountryiso(cursor.getString(colIdx));
            }else if(column.equals("transcription")) {
                vo.setTranscription(cursor.getString(colIdx));
            }else if(column.equals("subscription_component_name")) {
                vo.setSubscription_component_name(cursor.getString(colIdx));
            }else if(column.equals("name")) {
                vo.setName(cursor.getString(colIdx));
            }else if(column.equals("matched_number")) {
                vo.setMatched_number(cursor.getString(colIdx));
            }else if(column.equals("subscription_id")) {
                vo.setSubscription_id(cursor.getString(colIdx));
            }else if(column.equals("photo_id")) {
                vo.setPhoto_id(cursor.getLong(colIdx));
            }else if(column.equals("normalized_number")) {
                vo.setNormalized_number(cursor.getString(colIdx));
            }else if(column.equals("_id")) {
                vo.setId(cursor.getLong(colIdx));
            }else if(column.equals("type")) {
                vo.setType(cursor.getInt(colIdx));
            }else if(column.equals("is_read")) {
                vo.setIs_read(cursor.getInt(colIdx));
            }else if(column.equals("spam_desc")) {
                vo.setSpam_desc(cursor.getString(colIdx));
            }else if(column.equals("spam_category")) {
                vo.setSpam_category(cursor.getString(colIdx));
            }
        }
    }
}
